package cn.geodata.dataview.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

import cn.geodata.dataview.Fire;
import cn.geodata.dataview.FireFactory;
import cn.geodata.dataview.util.Util;
import cn.geodata.service.user.UserInstance;

public class ImageDownload {
	private String id;
	private InputStream image;
	private String fileName;
	
	public String getFileName() {
		return fileName;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}
	
	public InputStream getImage() {
		return image;
	}
	
	public String execute() throws Exception {
		Fire _fire = (Fire) (new FireFactory()).load(this.id, new UserInstance("download", "127.0.0.1"));
		
		File _path = null;
		for(String _dir : Util.getRepositoryPaths()){
			File _temp = new File(new File(_dir), _fire.getDir());
			if(_temp.exists()){
				_path = _temp;
				break;
			}
		}

		if(_path == null){
			throw new NullPointerException("Failed to find the dataset.");
		}
		
		this.image = new FileInputStream(_path);
		this.fileName = _path.getName();
		
		return "success";
	}
}
